using SmartLink.EnumLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartLink.Code
{
    public abstract class AbstractFilterRule
    {
        private Dictionary<ModuleCodeEnum, HashSet<AuthorizationScopeEnum>> _rules = new Dictionary<ModuleCodeEnum, HashSet<AuthorizationScopeEnum>>();

        public Dictionary<ModuleCodeEnum, HashSet<AuthorizationScopeEnum>> Rules { get { return _rules; } }

        /// <summary>
        /// 详细配置单个模块可配置的数据授权范围规则项
        /// </summary>
        /// <param name="moduleCode">模块编码</param>
        /// <param name="scopes">单个或多个数据授权范围项</param>
        protected void CreateRule(ModuleCodeEnum moduleCode, params AuthorizationScopeEnum[] scopes)
        {
            _rules.Add(moduleCode, new HashSet<AuthorizationScopeEnum>(scopes));
        }

        /// <summary>
        /// 快捷配置单个模块可配置的数据授权范围规则项
        /// </summary>
        /// <param name="moduleCode">模块编码</param>
        /// <param name="modes">单个或多个数据授权模式项</param>
        protected void CreateRule(ModuleCodeEnum moduleCode, params AuthorizeModeEnum[] modes)
        {
            if (modes != null && modes.Count() > 0)
            {
                var hs = new HashSet<AuthorizationScopeEnum>();
                foreach (var mode in modes)
                {
                    switch (mode)
                    {
                        case AuthorizeModeEnum.Creator:
                            hs.Add(AuthorizationScopeEnum.Self);
                            //hs.Add(AuthorizationScopeEnum.SelfAndSubordinates);
                            break;
                        case AuthorizeModeEnum.OID:
                            hs.Add(AuthorizationScopeEnum.Root);
                            hs.Add(AuthorizationScopeEnum.Organization);
                            hs.Add(AuthorizationScopeEnum.CustomOrganization);
                            break;
                        case AuthorizeModeEnum.OID_ODID:
                            hs.Add(AuthorizationScopeEnum.Root);
                            hs.Add(AuthorizationScopeEnum.Organization);
                            hs.Add(AuthorizationScopeEnum.CustomOrganization);
                            hs.Add(AuthorizationScopeEnum.Department);
                            hs.Add(AuthorizationScopeEnum.CustomDepartment);
                            break;
                        case AuthorizeModeEnum.ODID:
                            hs.Add(AuthorizationScopeEnum.Department);
                            hs.Add(AuthorizationScopeEnum.CustomDepartment);
                            break;
                    }
                }
                _rules.Add(moduleCode, hs);
            }
        }
    }
}
